package com.example.server;

import com.example.config.getHttpSessionConfig;
import com.example.domain.Message;
import com.example.util.MessageUtil;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint(value = "/chat",configurator = getHttpSessionConfig.class)
@Component
public class EndPoint {
    public static final HashMap<String,Session> map  = new HashMap<String,Session>();
    HttpSession httpSession = null;

    @OnOpen //在连接开始时
    public void onOpen(Session session,EndpointConfig config) throws IOException {
        if (map.size()<=250){
            //获取httpSession
            this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
            //存放在map集合
            String userName = (String) httpSession.getAttribute("userName");
            map.put(userName,session);
            System.out.println(userName+"已连接....");
            //广播消息，将所有登录成员信息发送给所有人
            String message = MessageUtil.getMessage(null,  null, null, map.keySet());
            broadcastUser(message);
        }
    }
    private void broadcastUser(String message) throws IOException {
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            Session session = map.get(key);
            //发送同步信息
            session.getBasicRemote().sendText(message);
        }
    }
    @OnMessage
    public void OnMessage(String message) throws IOException {
        Message mes = MessageUtil.fromMessage(message);
        System.out.println("接收到数据"+mes);
        if (mes.getTo().equals("all")){
            mes.setText("广播："+mes.getText());
            String mess = MessageUtil.getMessage(mes.getFrom(), mes.getTo(), mes.getText(), mes.getSet());
            String name = (String) httpSession.getAttribute("userName");
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                if (name.equals(key)) continue;
                Session session = map.get(key);
                //发送同步信息
                session.getBasicRemote().sendText(mess);
            }
        }else{
            Session session = map.get(mes.getTo());
            if (session!=null) {
                mes.setText("私聊："+mes.getText());
                String mess = MessageUtil.getMessage(mes.getFrom(), mes.getTo(), mes.getText(), mes.getSet());
                session.getBasicRemote().sendText(mess);
                System.out.println(message+"--数据已发送.....");
            }
        }
    }

    @OnClose
    public void OnClose(Session session) throws IOException {
        String name = (String) httpSession.getAttribute("userName");
        map.remove(name);
        //更新在线人数
        String message = MessageUtil.getMessage(null, null, null,  map.keySet());
        broadcastUser(message);
    }
}
